package com.ems.remote;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.FixedRecvByteBufAllocator;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.bytes.ByteArrayDecoder;
import io.netty.handler.codec.bytes.ByteArrayEncoder;
import io.netty.handler.timeout.IdleStateHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @Author: Wuzh
 * @Description:
 * @Date Created in 23:15 2023-12-28
 * @return
 */

@Component
public class ServerChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Resource
    private ServerChannelRequest serverChannelRequest;

    @Override
    protected void initChannel ( SocketChannel socketChannel ) {
        socketChannel.config ().setRecvByteBufAllocator ( new FixedRecvByteBufAllocator ( 1600 ) );
        ChannelPipeline pipeline = socketChannel.pipeline ();

        //IdleStateHandler心跳机制,如果超时触发Handle中userEventTrigger()方法
        pipeline.addLast ( "idleStateHandler",
                new IdleStateHandler ( 15, 0, 0, TimeUnit.MINUTES ) );
        // 字符串编解码器
        pipeline.addLast (
                new ByteArrayDecoder (),
                new ByteArrayEncoder ()
        );
        // 自定义Handler
        pipeline.addLast ( "serverChannelRequest", serverChannelRequest );
        //pipeline.addLast ( "serverChannelResponse", serverChannelResponse );
    }

}
